/**
 * @file skoll.actions.topbar.js
 * @author Alejandro Dario Simi
 * @date $Date: 2014-06-03 03:40:32 +0000 (Tue, 03 Jun 2014) $
 *
 * $Id: skoll.actions.topbar.js 112 2014-06-03 03:40:32Z daemonraco@gmail.com $
 * $URL: http://wcomix.googlecode.com/svn/trunk/skins/Skoll/js/skoll.actions.topbar.js $
 */

Skoll.actions.topbar = {
	//
	// Protected properties.
	//
	// Public methods.
	closeMenues: function() {
		$("#TopBarMenu").remove();
	},
	openHome: function(button) {
		Skoll.tools.preventDefault();

		Skoll.actions.topbar.closeMenues();
		Skoll.tools.reloadHome();
	},
	openLeftMenu: function(code, button) {
		Skoll.tools.Loading.start();

		Skoll.tools.preventDefault();

		Skoll.actions.topbar.closeMenues();

		var x = $(button).offset().left;
		var y = $(button).height();
		$.ajax({
			url: "?action=menu&code=" + code + "&nolayout&group=" + GROUP_ID + Skoll.tools.extraUrlParams(),
			success: function(data) {
				Skoll.actions.topbar._openMenu(data, x, y);
				Skoll.tools.Loading.stop();
			},
			error: function(data) {
			}
		});

		return false;
	},
	openRightUser: function(code, button) {
		Skoll.tools.Loading.start();

		Skoll.tools.preventDefault();

		Skoll.actions.topbar.closeMenues();

		var x = $(button).offset().left;
		var y = $(button).height();
		$.ajax({
			url: "?action=menu&code=" + code + "&nolayout&group=" + GROUP_ID + Skoll.tools.extraUrlParams(),
			success: function(data) {
				Skoll.actions.topbar._openMenu(data, x, y);
				Skoll.tools.Loading.stop();
			},
			error: function(data) {
			}
		});

		return false;
	},
	setPath: function(path) {
		$("#TopBarPath").text(path);
	},
	//
	// Protected methods.
	_bindButtons: function() {
		$(document).on("click", ".wc-topbar-home", function() {
			Skoll.actions.topbar.openHome($(this));
		});
		$(document).on("click", ".wc-topbar-menu-main", function() {
			Skoll.actions.topbar.openLeftMenu("main", $(this));
		});
		$(document).on("click", ".wc-topbar-menu-shortcuts", function() {
			Skoll.actions.topbar.openLeftMenu("shortcuts", $(this));
		});
		$(document).on("click", ".wc-topbar-menu-user", function() {
			Skoll.actions.topbar.openRightUser("user", $(this));
		});
		$(document).on("click", ".wc-topbar-menu-admin", function() {
			Skoll.actions.topbar.openLeftMenu("admin", $(this));
		});
	},
	_openMenu: function(data, x, y) {
		$("#TopBar").append('<div id="TopBarMenu"></div>');

		$("#TopBarMenu").html(data);

		var menu = $("#TopBarMenu .Menu");
		$(menu).css({
			position: "absolute",
			top: y + "px",
			left: x + "px"
		});

		var menuWidth = 0;
//		var menuHeight = 0;
		menu.each(function() {
			menuWidth = this.clientWidth;
//			menuHeight = this.clientHeight;
		});

		var changeCss = false;
		var changedCss = {};
		if ((x + menuWidth) > $(window).width()) {
			changedCss.position = "absolute";
			changedCss.top = y + "px";
			changedCss.left = ($(window).width() - menuWidth) + "px";

			changeCss = true;
		}
//		if ((y + menuHeight) > $(window).height()) {
//			changedCss.position = "fixed";
//
//			changeCss = true;
//		}

		if (changeCss) {
			$(menu).css(changedCss);
		}
	}
};

$(document).ready(function() {
	Skoll.actions.topbar._bindButtons();
	$("body").on("click", function(e) {
		Skoll.actions.topbar.closeMenues();
	});
});